package vn.com.boko.book.dao;

import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;

/**
 * @author Asus
 * 
 */
public class DaoGenerator {
	public static final int DATABASE_VERSION = 1;

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Schema schema = new Schema(DATABASE_VERSION, DaoGenerator.class
				.getPackage().getName());

		Entity website = addWebsite(schema);
		Entity manga = addManga(schema, website);
		addChapter(schema, manga);

		new de.greenrobot.daogenerator.DaoGenerator().generateAll(schema,
				"../BokoBook/src-dao");
	}

	/**
	 * @param schema
	 * @return
	 */
	private static Entity addWebsite(Schema schema) {
		Entity website = schema.addEntity("Website");
		website.addIdProperty();
		website.addStringProperty("title").notNull();
		website.addStringProperty("url");
		website.addBooleanProperty("onBar");
		website.addBooleanProperty("downloaded");
		return website;
	}

	/**
	 * @param schema
	 * @param website
	 * @return
	 */
	private static Entity addManga(Schema schema, Entity website) {
		Entity manga = schema.addEntity("Manga");
		manga.addIdProperty();
		manga.addStringProperty("title").notNull();
		manga.addStringProperty("description");
		manga.addStringProperty("url").notNull();
		Property fkProperty = manga.addLongProperty("websiteId").notNull()
				.getProperty();
		manga.addToOne(website, fkProperty);
		return manga;
	}

	/**
	 * @param schema
	 * @param manga
	 * @return
	 */
	private static Entity addChapter(Schema schema, Entity manga) {
		Entity chapter = schema.addEntity("Chapter");
		chapter.addIdProperty();
		chapter.addStringProperty("ordinal").notNull();
		chapter.addStringProperty("title");
		chapter.addStringProperty("url").notNull();
		Property fkProperty = chapter.addLongProperty("mangaId").notNull()
				.getProperty();
		chapter.addToOne(manga, fkProperty);
		return chapter;
	}
}
